// SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
// SPDX-License-Identifier: Mulan PSL v2
package entity

import "fmt"

const (
	SuccessCode ReturnCode = "0000000"

	NotebookErrorModule            = "31"
	CodeParamInvalid    ReturnCode = NotebookErrorModule + "A0001"
	CodeSystemError     ReturnCode = NotebookErrorModule + "B0002"
	CodeK8sFieldError   ReturnCode = NotebookErrorModule + "B0003"
)

type ReturnCode string

// 返回的结构体
type HttpRespond struct {
	Code    ReturnCode  `json:"code"`
	Msg     string      `json:"msg"`
	Success bool        `json:"success"`
	Data    interface{} `json:"data"`
}

// 返回的列表的结构体
type HttpRespondList struct {
	List  interface{} `json:"list"`
	Total int         `json:"total"`
}

// 错误处理的结构体
type AIPError struct {
	Code ReturnCode `json:"code"`
	Msg  string     `json:"msg"`
	Err  error      `json:"-"`
}

func (s *AIPError) Error() string {
	return s.Msg
}

func NewError(code ReturnCode, msg string) *AIPError {
	return &AIPError{code, msg, nil}
}

func NewErrorf(code ReturnCode, format string, a ...interface{}) *AIPError {
	return &AIPError{code, fmt.Sprintf(format, a...), nil}
}

func NewCodeError(code ReturnCode) *AIPError {
	msgMap := map[ReturnCode]string{
		CodeParamInvalid: MsgParamInvalid,
		MsgSystemError:   MsgSystemError,
	}
	msg := msgMap[code]
	return &AIPError{code, msg, nil}
}
